<?php

class m_jenisbarang extends CI_Model {

    function __construct() {
        // Call the Model constructor
        parent::__construct();
    }

    function get_count_all_jenisbarang() {
        $sql = "SELECT COUNT(jenisbarang_cd)'total' FROM jenisbarang";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return array();
        }
    }

    function get_count_all_jenisbarang_search($params) {
        $sql = "SELECT COUNT(jenisbarang_cd)'total' FROM jenisbarang
                WHERE jenisbarang_name LIKE ? AND kategoribarang_cd LIKE ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return array();
        }
    }

    //cek data
    function get_count_all_jenisbarang_by_kode($params) {
        $sql = "SELECT COUNT(*) 'total' FROM jenisbarang WHERE jenisbarang_cd = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return array();
        }
    }

    //cek data
    function get_count_all_jenisbarang_by_jenisbarang_cd_kategoribarang_cd($params) {
        $sql = "SELECT COUNT(*) 'total' FROM jenisbarang WHERE jenisbarang_cd = ? AND kategoribarang_cd = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return array();
        }
    }

    //cek data
    function get_count_jenisbarang_by_kategoribarang_cd($params) {
        $sql = "SELECT COUNT(*) 'total' FROM jenisbarang WHERE kategoribarang_cd = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return array();
        }
    }

    // get user profil
    function get_all_jenisbarang() {
        $sql = "SELECT a.*FROM jenisbarang a               
                ORDER BY a.jenisbarang_name ASC";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    function get_all_jenisbarang_pagination($params) {
        $sql = "SELECT a.*, b.kategoribarang_name FROM jenisbarang a
                JOIN kategoribarang b ON a.kategoribarang_cd =b.kategoribarang_cd
                ORDER BY b.kategoribarang_name, CAST(a.jenisbarang_cd AS UNSIGNED) ASC
                LIMIT ?, ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    function get_all_jenisbarang_pagination_search($params) {
        $sql = "SELECT a.*, b.kategoribarang_name FROM jenisbarang a
                JOIN kategoribarang b ON a.kategoribarang_cd =b.kategoribarang_cd
                WHERE a.jenisbarang_name LIKE ? AND a.kategoribarang_cd LIKE ?
                ORDER BY b.kategoribarang_name, CAST(a.jenisbarang_cd AS UNSIGNED) ASC
                LIMIT ?, ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    function get_jenisbarang_by_id($params) {
        $sql = "SELECT a.*, b.kategoribarang_name FROM jenisbarang a
                JOIN kategoribarang b ON a.kategoribarang_cd =b.kategoribarang_cd
                WHERE a.jenisbarang_cd = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    function get_jenisbarang_by_jenisbarang_cd_kategoribarang_cd($params) {
        $sql = "SELECT a.*, b.kategoribarang_name FROM jenisbarang a
                JOIN kategoribarang b ON a.kategoribarang_cd =b.kategoribarang_cd
                WHERE a.jenisbarang_cd = ? AND b.kategoribarang_cd = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    function get_jenisbarang_by_kategoribarang($params) {
        $sql = "SELECT a.*, b.kategoribarang_name FROM jenisbarang a
                JOIN kategoribarang b ON a.kategoribarang_cd =b.kategoribarang_cd
                WHERE a.kategoribarang_cd = ?
                ORDER BY CAST(jenisbarang_cd AS UNSIGNED) ASC";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    // insert
    function insert($user_id) {
        // update by this date
        $sql = "INSERT INTO jenisbarang VALUES (?, ?, ?, ?, NOW())";
        return $this->db->query($sql, $user_id);
    }

    // edit
    function edit($user_id) {
        // update by this date
        $sql = "UPDATE jenisbarang SET jenisbarang_name = ?, mdb = ? WHERE jenisbarang_cd = ? AND kategoribarang_cd = ?";
        return $this->db->query($sql, $user_id);
    }

    // delete
    function delete($user_id) {
        // update by this date
        $sql = "DELETE FROM jenisbarang WHERE jenisbarang_cd = ? AND kategoribarang_cd = ?";
        return $this->db->query($sql, $user_id);
    }

    function get_jenis_by_kategoribarang_cd_laporan($param) {
        $sql = "SELECT a.*, b.*, c.* FROM jenisbarang a
                JOIN kategoribarang b ON a.kategoribarang_cd = b.kategoribarang_cd
                JOIN group_barang c ON b.gpbarang_cd = c.gpbarang_cd
                WHERE a.kategoribarang_cd = ?";
        $query = $this->db->query($sql, $param);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }
    
    function get_barang_by_id($params) {
        $sql = "SELECT * FROM jenisbarang WHERE jenisbarang_cd = ? AND kategoribarang_cd = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

}
